import 'package:shared_preferences/shared_preferences.dart';

class StorageClient {
  static final searchReault = 'SEARCHRESULT';

  /// 获取数据
  static Future getHist() async {
    final storageClient = await SharedPreferences.getInstance();

    var rsp = storageClient.getStringList(searchReault);
    print('+++++++++++++');
    print(rsp);
    if (rsp != null) {
      return rsp;
    } else {
      return null;
    }
  }

  /// 存储数据
  static Future setHis(String value) async {
    final storageClient = await SharedPreferences.getInstance();

    // 首先取出判断有没有这条数据
    var rspGet = storageClient.getStringList(searchReault);
    print('获取厨具');
    print(rspGet.runtimeType);

    if (rspGet != null) {
      if (rspGet!.contains(value)) {
        print('不做处理');
      } else {
        rspGet.add(value);
        print(rspGet);
        // 并存储
        var rsp = await storageClient.setStringList(searchReault, rspGet);
        print(rsp);
      }
    } else {
      await storageClient.setStringList(searchReault, [value]);
    }
    // 已经包含这条数据
    if (rspGet!.contains(value)) {
      print('不做处理');
    } else {
      rspGet.add(value);
      print(rspGet);
      // 并存储
      var rsp = await storageClient.setStringList(searchReault, rspGet);
      print(rsp);
    }

    return null;
  }

  static Future deleteAll({String? key}) async {
    final storageClient = await SharedPreferences.getInstance();
    if (key == null) {
      storageClient.clear();
    } else {
      storageClient.remove(key);
    }
  }
}
